﻿using System;
using System.Windows;
using System.Windows.Controls;

using Hetymine.Executioner.Commands;

namespace Hetymine.Executioner.Plugins.Lua {
    /// <summary>
    /// Interaction logic for LuaCommandConfig.xaml
    /// </summary>
    public partial class LuaCommandConfig : UserControl, ICommandConfigurator<ICommand> {
        private bool _IsApply = false;
        private LuaCommand _LuaCommand;

        public LuaCommandConfig() {
            InitializeComponent();

            this.cmdName.TextChanged += new TextChangedEventHandler(cmdName_TextChanged);
            this.cmdAudioInput.Click += new RoutedEventHandler(cmdAudioInput_Click);
            this.cmdScript.TextChanged += new TextChangedEventHandler(cmdScript_TextChanged);
            this.cmdParams.TextChanged += new TextChangedEventHandler(cmdParams_TextChanged);
            this.cmdDelimiter.TextChanged += new TextChangedEventHandler(cmdDelimiter_TextChanged);
        }

        private void cmdName_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._LuaCommand == null) { return; }

            this._LuaCommand.Name = this.cmdName.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdAudioInput_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply || this._LuaCommand == null) { return; }

            if (this.cmdAudioInput.IsChecked == true) {
                this._LuaCommand.DisableInput("string");
                this._LuaCommand.EnableInput("audio");
            }
            else if (this.cmdAudioInput.IsChecked == null) {
                this._LuaCommand.EnableInput("audio");
                this._LuaCommand.EnableInput("string");
            }
            else {
                this._LuaCommand.DisableInput("audio");
                this._LuaCommand.EnableInput("string");
            }

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdScript_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._LuaCommand == null) { return; }

            this._LuaCommand.Script= this.cmdScript.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdParams_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._LuaCommand == null) { return; }

            this._LuaCommand.Parameters = this.cmdParams.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));                        
        }
        private void cmdDelimiter_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._LuaCommand == null) { return; }

            this._LuaCommand.Delimiter = this.cmdDelimiter.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }

        private string CommandName {
            get {
                if (this._LuaCommand.IsEmpty) {
                    return string.Empty;
                }

                return this._LuaCommand.ToString();
            }
        }

        #region ICommandConfigurator<ICommand> Members
        public void InitializeConfig(ICommand config) {
            this._IsApply = false;

            if (config == null) {
                this._LuaCommand = null;
                
                this.cmdName.Text = string.Empty;
                this.cmdAudioInput.IsChecked = false;
                this.cmdScript.Text = string.Empty;
                this.cmdParams.Text = string.Empty;
                this.cmdDelimiter.Text = string.Empty;
            }
            else {
                this._LuaCommand = config as LuaCommand;

                this.cmdName.Text = this._LuaCommand.Name;

                if (this._LuaCommand.EnabledInputs.Contains("audio")) {
                    if (this._LuaCommand.EnabledInputs.Contains("string")) {
                        this.cmdAudioInput.IsChecked = null;
                    }
                    else {
                        this.cmdAudioInput.IsChecked = true;
                    }
                }
                else {
                    this.cmdAudioInput.IsChecked = false;
                }

                this.cmdScript.Text = this._LuaCommand.Script;
                this.cmdParams.Text = this._LuaCommand.Parameters;
                this.cmdDelimiter.Text = this._LuaCommand.Delimiter;
            }

            this._IsApply = true;
        }

        public UserControl ConfigPanel { get { return this; } }

        public event CommandNameEventHandler CommandNameChanged;
        protected virtual void OnCommandNameChanged(object sender, CommandNameEventArgs e) {
            if (this.CommandNameChanged != null) {
                this.CommandNameChanged(sender, e);
            }
        }
        #endregion
    }
}
